package Serialize;

import java.io.*;
import java.util.Scanner;

/**
 * @Description:简单示例
 * @Author:AMark
 * @Date: 11/3/2023
 */
public class test {
    public static void main(String[] args) {
        ObjectOutputStream oos;
        ObjectInputStream ois;
        int count;
        Scanner sc = new Scanner(System.in);
        try {
            oos = new ObjectOutputStream(new FileOutputStream("E:\\Obsidian\\CS\\CS Code\\Java\\learning-java\\IOStream\\src\\Serialize\\student.txt"));
            System.out.println("请输入学生人数");
            count = sc.nextInt();
            oos.writeInt(count);
            Students[] stu = new Students[count];
            System.out.println("请输入学生的姓名、院系、年龄、成绩");
            for (int i = 0; i < count; i++) {
                stu[i] = new Students(sc.next(), sc.next(), sc.nextInt(), sc.nextDouble());
                oos.writeObject(stu[i]);
            }
            oos.close();
        } catch (FileNotFoundException e) {
            System.out.println("没有找到文件");
        } catch (IOException e) {
            System.out.println("文件读写有误");
        }
        try {
            ois = new ObjectInputStream(new FileInputStream("E:\\Obsidian\\CS\\CS Code\\Java\\learning-java\\IOStream\\src\\Serialize\\student.txt"));
            count = ois.readInt();
            Students[] stu = new Students[count];
            for (int i = 0; i < count; i++) {
                stu[i] = (Students) ois.readObject();
                stu[i].print();
            }
            ois.close();
        } catch (FileNotFoundException e) {
            System.out.println("没有找到文件");
        } catch (IOException e) {
            System.out.println("文件读写出错");
        } catch (ClassNotFoundException e) {
            System.out.println("未找到类");
        }
    }
}
